<?php

	/**
	 * File : list_view.php
	 * Description : Définition et fonctions du composant list_view
	 * Date : 07.08.2012 15:08:18
	 * 
	 * This file is part of Phenix.  Phenix is free software: you can
	 * redistribute it and/or modify it under the terms of the GNU General Public
	 * License as published by the Free Software Foundation, version 2.
	 *
	 * This program is distributed in the hope that it will be useful, but WITHOUT
	 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
	 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
	 * details.
	 *
	 * You should have received a copy of the GNU General Public License along with
	 * this program; if not, write to the Free Software Foundation, Inc., 51
	 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
	 *
	 * Nils Schaetti <n.schaetti@gmail.com>
	**/

	// Colonne
	require_once('list_view_column.php');
	
	// Ligne
	require_once('list_view_line.php');

	/*
	 * Classe représentant une ListView
	 */
	class ktkListView
	{
	
		/***************
		 * Propriétés
		 ***************/

		// Les colones de la liste
		private $Columns = array();
		 
		// Les lignes
		private $Lines = array();
		 
		// Nom de la liste
		private $Name;
		 
		/****************
		 * Fonctions
		 ****************/
		 
		/*
		 * Constructeur
		 */
		public function ktkListView($Name)
		{
			$this->Name = $Name;
		}
		
		/*
		 * Ajoute une colone
		 */
		public function addColumn($name, $id, $type)
		{
			// Nouvelle colonne
			$column = new ktkListViewColumn($name, $this->Name.$id, $type);
		
			// Ajoute
			$this->Columns[] = $column;
		}
		
		/*
		 * Renvoi le code HTML
		 */
		public function getCode()
		{
			// Entête
			$code = $this->getHeaderCode();
			
			// Liste
			$code .= $this->getListCode();
			
			// Fin
			$code .= '</table>';
			
			// Return
			return $code;
		}
		
		/*
		 * Renvoi le code de l'en-tête
		 */
		private function getHeaderCode()
		{
			$cells_code = '';
			$titleID = $this->Name.'TrTitle';
			$tableID = $this->Name.'Table';
			$code = <<< __DELIMITER__
			<table cellpadding="0" cellspacing="0" class="ktkListView" id="$tableID">
				<tr class="ktkListViewTrHeader" id="$titleID">
					@@_CELLS_@@
				</tr>
__DELIMITER__;

			// Pour chaque colonnes
			foreach($this->Columns as $column)
				$cells_code .= $column->getCode();
			
			// Remplacement
			$code = str_replace('@@_CELLS_@@', $cells_code, $code);
			
			// Retourne
			return $code;
		}
		
		/*
		 * Renvoi le code de la liste
		 */
		private function getListCode()
		{
			// Code HTML
			$code = '';

			// Pour chaque ligne
			$n_line = 0;
			foreach($this->Lines as $Line)
			{
				// Nouvelle ligne
				$code .= '<tr class="ktkListViewTr" id="'.$this->Name.'Tr'.$n_line.'" draggable="true">';

				// Pour chaque colone
				$n_column = 0;
				foreach($this->Columns as $Column)
				{
					// ID de la colone
					$column_id = $Column->getId().$n_line;
					
					// Code
					if($Line->getLink($n_column) === '')
						$column_code = '<td class="@@_TYPE_@@" id="'.$column_id.'">@@_CONTENT_@@</td>';
					else
						$column_code = '<td class="@@_TYPE_@@" id="'.$column_id.'"><a href="@@_LINK_@@">@@_CONTENT_@@</a></td>';
				
					// Selon le type de la colonne
					switch($Column->getType())
					{
						case COLUMN_TYPE_TEXT:
							$id = 'ktkListViewTd';
							$content = $Line->getContent($n_column);
							break;
						case COLUMN_TYPE_IMAGE:
							$id = 'ktkListViewTdSmall';
							$content = '<img src="'.$Line->getContent($n_column).'">';
							break;
						case COLUMN_TYPE_CHECKBOX:
							$id = 'ktkListViewTdSmall';
							$content = '<input type="checkbox" name="'.$Line->getContent($n_column).'">';
							break;
					}
					
					// Remplacement
					$column_code = str_replace('@@_TYPE_@@', $id, $column_code);
					$column_code = str_replace('@@_CONTENT_@@', $content, $column_code);
					$column_code = str_replace('@@_LINK_@@', $Line->getLink($n_column), $column_code);
					
					// Ajoute
					$code .= $column_code;
					
					// Colonne suivante
					$n_column++;
				}

				// Fin de ligne
				$code .= '</tr>';
				
				// Ligne suivante
				$n_line++;
			}
			
			return $code;
		}
		
		/*
		 * Ajoute une ligne
		 */
		public function addLine($line)
		{
			$this->Lines[] = $line;
		}
	
	}

?>
